<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<jsp:useBean id="loginBean" class="de.fhhof.vinf.ssp.studienarbeit11.beans.LoginBean" scope="session"></jsp:useBean>
	
<%	
	Statement stmt;
	ResultSet res;
	Class.forName("com.mysql.jdbc.Driver");
	String url = "jdbc:mysql://localhost:3306/studienarbeit";
	Connection con = DriverManager.getConnection(url, "admin",
		"cisco");
	stmt = con.createStatement();
	
	Integer userid = (Integer)session.getAttribute("user_id");
	if (userid == null) {
		%>
		<form action="<%=response.encodeURL("../includejsp/checkLogin.jsp")%>"
			method="post">
			<table>
				<tr>
					<td>
						<table>
							<tr>
								<td><input type="text" name="email" value="E-Mail" size="20" />
								</td>
							</tr>
							<tr>
								<td><input type="password" name="password" value="Passwort"
									size="20" /></td>
							</tr>
		
						</table>
					</td>
					<td><input type="submit" value="Login" class="Button" />
					</td>
				</tr>
			</table>
		</form>
		<%	
			ArrayList<String> errors = (ArrayList<String>) session.getAttribute("login_errors");
			if(errors != null && errors.size() > 0){
				for(String error : errors){
					out.println(error + "<br />");
				}				
			}
			session.removeAttribute("login_errors");
		%>
		<a href="<%= response.encodeURL("../jsp/index.jsp?site=register")%>">Noch nicht registriert?</a>
		<%
	} else {
		String email ="";
		boolean isAdmin = false;
		try {
			res = stmt.executeQuery("SELECT email, is_admin FROM users WHERE user_id = " + userid.intValue() + ";");
			while(res.next()){
				email = res.getString("email");
				isAdmin = ((res.getInt("is_admin"))== 1) ? true : false;
			}		
		
		} catch (SQLException sqlE) {
			email = "SQL-Failure";
			con.close();
		} finally {
			con.close();
		}
		%>Hallo <%= email %>!<br/>
		<a href="<%= response.encodeURL("../jsp/index.jsp?site=profile")%>">Profil</a><br/>
		<%
			if(isAdmin){
				%><a href="<%= response.encodeURL("../jsp/index.jsp?site=admincenter")%>">Admincenter</a><br/><%
			}
		%>
		<a href="<%= response.encodeURL("../includejsp/doLogout.jsp")%>">Abmelden</a><%
	}
%>

